unit clsMotCancPedido;

interface

uses Dm,dbtables,biblio,StdCtrls;

type
  TMotCancPedido=Class(TObject)
    private
      Canp_codigo    : String;
    public
      Canp_descricao : String;
      function  Gravar(p_Canp_codigo:String):Byte;
      function  Recuperar(p_Canp_codigo:String):Byte;
      function  Excluir(p_Canp_codigo:String):Byte;
      function  Recuperar_Codigo(Descricao: String): string;
      function  Recuperar_Posicao(p_Canp_codigo: String): integer;
      function  Primeiro: string;
      function  Ultimo  : string;
      procedure Consultar(var p_q:TQuery;p_nome:String);
      procedure Alimenta_ComboBox(combo: TComboBox);
  end;

implementation

function TMotCancPedido.Gravar;

var tq:TQuery;

begin
  tq:=Tquery.Create(dm.DmInt);
  tq.databasename:=dm.dmint.Db.DatabaseName;
  tq.name:='qCanp_codigo';
  tq.active:=False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM MotCancPedido where Canp_codigo=:k_Canp_codigo');
  tq.parambyname('k_Canp_codigo').asstring:=strzero(p_Canp_codigo,3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('INSERT INTO MotCancPedido VALUES(:k_Canp_codigo,:k_Canp_descricao)');
  end else begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('UPDATE MotCancPedido SET Canp_descricao=:k_Canp_descricao WHERE Canp_codigo=:k_Canp_codigo');
  end;
  tq.parambyname('k_Canp_codigo').asstring:=strzero(p_Canp_codigo,3);
  tq.parambyname('k_Canp_descricao').asstring:=Canp_descricao;
  tq.close;
  try
    tq.execsql;
    result:=1;
  except
    result:=0;
  end;
  tq.Free;
end;

function TMotCancPedido.Recuperar;

var tq:TQuery;

begin
  tq:=Tquery.Create(dm.dmint);
  tq.databasename:=DmInt.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM MotCancPedido where Canp_codigo=:k_Canp_codigo');
  tq.Params[0].asstring:=strzero(p_Canp_codigo,3);
  tq.open;
  Canp_codigo      :=p_Canp_codigo;
  if tq.eof then begin
    Canp_descricao :='';
    Result:=0;
  end else begin
    Canp_descricao:=tq.fieldbyname('Canp_descricao').asstring;
    Result:=1;
  end;
  tq.Close;
  tq.Free;
end;

function TMotCancPedido.Excluir;

var tq:TQuery;

begin
  tq:=Tquery.Create(dm.dmint);
  tq.databasename:=dm.dmint.db.databasename;
  tq.name:='qCanp_codigo';
  tq.active:=False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM MotCancPedido WHERE Canp_codigo=:k_Canp_codigo');
  tq.parambyname('k_Canp_codigo').asstring:=strzero(p_Canp_codigo,3);
  try
    tq.execSQL;
    Result:=1;
  except
    result:=0;
  end;
  tq.close;
  tq.Free;
end;

procedure TMotCancPedido.Consultar;

begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from MotCancPedido where Canp_descricao>=:k_Canp_descricao order by Canp_descricao');
  p_q.parambyname('k_Canp_descricao').asstring:=p_nome;
end;

procedure TMotCancPedido.Alimenta_ComboBox;
var tq: TQuery;
begin
 tq             := TQuery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := false;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from MotCancPedido');
 tq.open;
 combo.clear;
 while not tq.eof do begin
  combo.Items.Add(tq.fieldbyname('Canp_Descricao').asstring);
  tq.next;
 end;
 tq.close;
 tq.free;
end;

function TMotCancPedido.Recuperar_Codigo;
var tq: TQuery;
begin
 tq             := TQuery.Create(dmint);
 tq.databasename:= DmInt.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from MotCancPedido where Canp_Descricao=:a');
 tq.Params[0].asstring:= Descricao;
 tq.open;
 result:= tq.fieldbyname('Canp_Codigo').asstring;
 tq.close;
 tq.free;
end;

function TMotCancPedido.Recuperar_Posicao;
var tq   : TQuery;
    pos,i: integer;
begin
 tq             := TQuery.Create(dmint);
 tq.databasename:= DmInt.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from MotCancPedido');
 tq.open;
 pos:= 0;
 i  := 0;
 while not tq.eof do begin
  if p_Canp_codigo=tq.fieldbyname('Canp_codigo').asstring then pos:= i;
  i:= i+1;
  tq.next;
 end;
 result:= pos;
 tq.close;
 tq.free;
end;

function TMotCancPedido.Primeiro;
var tq: TQuery;
begin
 tq             := TQuery.Create(dmint);
 tq.databasename:= DmInt.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select min(Canp_Codigo) primeiro from MotCancPedido');
 tq.open;
 result:= tq.fieldbyname('primeiro').asstring;
 tq.close;
 tq.free;
end;

function TMotCancPedido.Ultimo;
var tq: TQuery;
begin
 tq             := TQuery.Create(dmint);
 tq.databasename:= DmInt.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select max(Canp_Codigo) ultimo from MotCancPedido');
 tq.open;
 result:= tq.fieldbyname('ultimo').asstring;
 tq.close;
 tq.free;
end;

end.
